﻿#region Mortgage Controller
//
// All rights are reserved. Reproduction or transmission in whole or in part, in 
// any form or by any means, electronic, mechanical or otherwise, is prohibited 
// without the prior written consent of the copyright owner.
// 
// Filename: MortgageController.cs
//
#endregion 

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Controls;
using DiscoveryLogic.UI.Layouts.Controls;
using DiscoveryLogic.DataServices.DataSources;
using DiscoveryLogic.UI.Layouts.Controls;

namespace DiscoveryLogic.UI.Layouts.Controllers
{
    /// <summary>
    /// Controller object associated with the mortgage calculator UI control.
    /// This control contains references to all IGraphControl objects (Views) added to the mortgage calculator and
    /// the mortgage object representing the users mortgage account (Model).
    /// 
    /// The controller responds to change events raised by the model or view objects and
    /// notifies the Views that they need to alter to reflect the changes by the other controls.
    /// </summary>
    public class HelocController
    {
        #region Private Variables

            private List<IGraphControl> controlledControls;
            private MortgageAccount model;

        #endregion

        #region Constructors

            public HelocController()
            {
                controlledControls = new List<IGraphControl>();
                model = null;
            }

            public HelocController(MortgageAccount model)
                : this()
            {
                SetModel(model);
            }

        #endregion

        #region Public Methods

            /// <summary>
            /// Sets the current model (Mortgage Account).
            /// </summary>
            /// <param name="model">Mortgage Account</param>
            public void SetModel(MortgageAccount model)
            {
                this.model = model;
                if (model != null)
                {
                    model.DetailChanged += new Account.DetailChangedEventHandler(model_DetailChanged);
                }
            }

            /// <summary>
            /// Adds an IGraphControl to the list of controlled controls.
            /// </summary>
            /// <param name="controlToAdd">a control to control.</param>
            public void AddControl(IGraphControl controlToAdd) {
                controlToAdd.SliderChanged += new SliderChangedEventHandler(controlToAdd_SliderChanged);
                controlledControls.Add(controlToAdd);
            }

        #endregion

        #region Helper Methods

            private double CalculateChangeInPayments(double deltaYears)
            {
                //For Demo purpose only
                return deltaYears * 5.0;
            }

            private double CalculateChangeInYears(double deltaPayments)
            {
                //For Demo purpose only
                return deltaPayments / 5.0;
            }
            /// <summary>
            /// Event handler for controlled IGraphControls SliderChangedEvents.
            /// </summary>
            /// <param name="sender">IGraphControl raising the event</param>
            /// <param name="e">event args</param>
            private void controlToAdd_SliderChanged(object sender, EventArgs e)
            {
                // Current draw amount
                double drawAmount = ((IGraphControl)sender).GetLastSliderChange();

                if (controlledControls == null){
                    return;
                }

                foreach (IGraphControl control in controlledControls)
                {
                    if (control != sender &&
                        control.GetType() == typeof(HelocBarChartCylinders))
                    {
                        ((HelocBarChartCylinders)control).SetBarHeight(drawAmount);
                    }                                          
                } 
                
            }

            /// <summary>
            /// Event handler for data detail changes in the Mortgage Account( Model )
            /// </summary>
            /// <param name="sender">MortgageAccount raising the event</param>
            /// <param name="e">event args</param>
            private void model_DetailChanged(object sender, EventArgs e)
            {
                if (controlledControls != null)
                {
                    foreach (IGraphControl control in controlledControls)
                    {
                        control.SetSlider(-2);
                    }
                }
            }

        #endregion

    }
}
